/**
 * Created by xin on 2017/9/28.
 */

function initVideoElement() {
    var videoWrapperElements = document.querySelectorAll('.videoWrapper');

    var showFullscreenButton = document.fullscreenEnabled || document.mozFullscreenEnabled || document.webkitFullscreenEnabled ? true : false;

    for(var i = 0; i < videoWrapperElements.length; i++) {
        setUpVideoElement(videoWrapperElements[i]);
    }
}
function setUpVideoElement(videoWrapperElement) {
    var videoElement = videoWrapperElement.querySelector('video');
    var fullscreenBtn = videoWrapperElement.querySelector('.fullscreenBtn');
    if(fullscreenBtn) {
        if(showFullscreenButton) {
            fullscreenBtn.addEventListener('click', function () {
                // Following code from: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode
                if (videoElement.requestFullscreen) {
                    videoElement.requestFullscreen();
                } else if (videoElement.msRequestFullscreen) {
                    videoElement.msRequestFullscreen();
                } else if (videoElement.mozRequestFullScreen) {
                    videoElement.mozRequestFullScreen();
                } else if (videoElement.webkitRequestFullscreen) {
                    videoElement.webkitRequestFullscreen();
                }
            });
        } else {
            fullscreenBtn.style.display = 'none';
        }
    }

    videoElement.addEventListener('click', function(evt) {
        if (evt.target.paused) {
            evt.target.play();
        } else {
            evt.target.pause();
        }
    });
}